package sort;

public class BubbleSort {
    public static void main(String[] args) {
        int res[] = {6,1,2,7,9,3,4,5,10,7,8};
        bubbleSort(res);
        for (int item : res) {
            System.out.println(item);
        }
    }
    public static void bubbleSort(int[] arr){
        if(arr==null||arr.length==0) return;
        int len = arr.length;
        for(int i = 0;i<len-1;i++){
             boolean swapped = false; // 优化：标记本轮是否有交换
            for(int j=0;j<len-1-i;j++){
                if(arr[j]>arr[j+1]){
                    int tem = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tem;
                    swapped = true;
                }
            }
            if (!swapped) {
                break;
            }
        }
    }
}
